<?
session_start();
include("includes/session.php");
if($get == 'event'){
	if($do == 'list'){
		$json = array();
		$sql = mysql_query("SELECT * FROM `event` ORDER BY `id`");
		echo '[';
		while($rs = mysql_fetch_object($sql)){
			$allDay = 'false';
			if($rs->allDay == 1){
				$allDay = 'true';
			}
			$start = date('Y-m-d H:i:s', $rs->start);
			$end = date('Y-m-d H:i:s', $rs->end);
			$str .= ',{"id":"'.$rs->id.'","title":"'.$rs->title.'","start":"'.$start.'","end":"'.$end.'","className":"'.$rs->className.'","allDay":'.$allDay.'}';
		}
		echo substr($str, 1);
		echo ']';
	}
	elseif($do == 'add'){
		$title = $_REQUEST['title'];
		$start = strtotime($_REQUEST['start']);
		$end = strtotime($_REQUEST['end']);
		$className = $_REQUEST['className'];
		mysql_query("INSERT INTO `event` (`title`, `start`, `end`, `className`) VALUES ('$title','$start','$end','$className')");
	}
	elseif($do == 'update'){
		$id = $_REQUEST['id'];
		$title = $_REQUEST['title'];
		$allDay = '1';
		if($_REQUEST['allDay'] == false){
			$allDay = '0';
		}
		mysql_query("UPDATE `event` SET `title` = '$title', `allDay` = '$allDay' WHERE `id` = $id");
	}
	elseif($do == 'delete'){
		$id = $_REQUEST['id'];
		mysql_query("DELETE FROM `event` WHERE `id` = $id");
	}
	elseif($do == 'done'){
		$id = $_REQUEST['id'];
		mysql_query("UPDATE `event` SET `status` = 1 WHERE `id` = $id");
	}
}
elseif($get == 'contract'){
	if($do == 'display'){
		$number = $_REQUEST['number'];
		mysql_query("UPDATE `setting` SET `value` = '$number' WHERE `name` = 'contract_display'");
	}
	elseif($do == 'delete'){
		$id = $_REQUEST['id'];
		mysql_query("DELETE FROM `contract` WHERE `id` = $id");
		mysql_query("DELETE FROM `product` WHERE `cid` = $id");
	}
	elseif($do == 'datebb'){
		$value = date('d/m/Y', ($_REQUEST['value'])/1000);
		//$value = $_REQUEST['value'];
		$id = $_REQUEST['id'];
		mysql_query("UPDATE `contract` SET `datebb` = '$value' WHERE `id` = '$id'");
	}
	elseif($do == 'status'){
		$id = $_REQUEST['id'];
		$status = mysql_result(mysql_query("SELECT `status` FROM `contract` WHERE `id` = '$id'"),0,0) + 1;
		if($status == 5){
			$status = 0;
		}
		if(isset($_REQUEST['display'])){
			$display = $_REQUEST['display'];
			if($status == 0){
				$status = 4;
			}
			else{
				$status = $status - 1;
			}
			$class = contract_status_class($status);
			$status_name = contract_status($status);
			echo '
					<span style="cursor:pointer;" id="status'.$display.'_'.$id.'" class="'.$class.'">'.$status_name.'</span>
					<script>
						$("#status'.$display.'_'.$id.'").click(function(){
							$.ajax({
								url: "ajax?get=contract&do=status",
								data: "id='.$id.'",
								type: "POST",
								success: function(json) {
									$("#display_status'.$display.'_'.$id.'").load("ajax?get=contract&do=status&id='.$id.'&display='.$display.'").hide().fadeIn("slow");
									';
			if($display == 1){
				echo '$("#display_status2_'.$id.'").load("ajax?get=contract&do=status&id='.$id.'&display=2").hide().fadeIn("slow");
				';
			}
			echo '				}
							});
						});
					</script>

				';
		}
		else{
			mysql_query("UPDATE `contract` SET `status` = '$status' WHERE `id` = '$id'");
		}
	}
	elseif($do == 'update'){
		$cnum = 0;
		$uContract = array();
		foreach($_POST as $key => $value){
			$uContract[$cnum] = $value;
			$cnum++;
		}
		$cProduct = 0;
		$check = true;
		$product = mysql_result(mysql_query("SELECT `product` FROM `contract` WHERE `id` = $uContract[0]"),0,0);
		$product_name = $uContract[13];
		$product_page = $uContract[14];
		$form = $uContract[15];
		$serial = $uContract[16];
		$logo = $uContract[17];
		$hoavan = $uContract[18];
		$signed = $uContract[19];
		$matma = $uContract[20];
		$color = $uContract[21];
		$bowel = $uContract[22];
		$size = $uContract[23];
		$unit = $uContract[24];
		$quantity = $uContract[25];
		$quantity_t = $uContract[26];
		$quantity_d = $uContract[27];
		$price = $uContract[28];
		//viết hàm check các thứ
		foreach($product_name as $key => $value){
			$order = $cProduct + 1;
			$cPrice = str_replace(',', '', $price[$cProduct]);
			if(($quantity[$cProduct] != '') || ($quantity[$cProduct] != 0)){
				$cquantity = str_replace(',', '', $quantity[$cProduct]);
				$quantity_t = 1;
				$quantity_d = qty_d($unit[$cProduct], $cquantity);
			}
			else{
				$quantity_t = $quantity_t[$cProduct];
				$quantity_d = $quantity_d[$cProduct];
				$cquantity = qty_check($quantity_t, $quantity_d, $unit[$cProduct]);
				if($cquantity == false){
					$check = false;
					header('Content-type: application/json');
					echo json_encode(array('return'=>'quantity_t_'.$order));
					exit();
				}
			}
			$sohuy = sohuy($product_page[$cProduct], $quantity_t, $quantity_d);
			$banhuy = banhuy($product_page[$cProduct], $color[$cProduct]);
			if($check == true){
				if($cProduct < $product){
					mysql_query("UPDATE `product` SET `sohuy` = '$sohuy', `banhuy` = '$banhuy', `product_name` = '$value', `product_page` = '$product_page[$cProduct]', `form` = '$form[$cProduct]', `serial` = '$serial[$cProduct]', `logo` = '$logo[$cProduct]', `hoavan` = '$hoavan[$cProduct]', `signed` = '$signed[$cProduct]', `matma` = '$matma[$cProduct]', `color` = '$color[$cProduct]', `bowel` = '$bowel[$cProduct]', `size` = '$size[$cProduct]', `unit` = '$unit[$cProduct]', `quantity` = '$cquantity', `price` = '$cPrice', `quantity_t` = '$quantity_t', `quantity_d` = '$quantity_d' WHERE `cid` = $uContract[0] AND `order` = '$order'");
				}
				else{
					$nProduct = $product + 1;
					mysql_query("UPDATE `contract` SET `product` = '$nProduct' WHERE `id` = $uContract[0]");
					mysql_query("INSERT INTO `product` (`id` ,`cid` ,`form` ,`serial` ,`quantity` ,`unit` ,`product_name` ,`product_page` ,`price` ,`color` ,`bowel` ,`size` ,`signed` ,`sohuy` ,`banhuy` ,`quantity_t` ,`quantity_d` ,`hoavan` ,`logo` ,`matma` ,`order`) VALUES (NULL, '$uContract[0]', '$form[$cProduct]', '$serial[$cProduct]', '$quantity[$cProduct]', '$unit[$cProduct]', '$value', '$product_page[$cProduct]', '$price[$cProduct]', '$color[$cProduct]', '$bowel[$cProduct]', '$size[$cProduct]', '$signed[$cProduct]', '$sohuy', '$banhuy', '$quantity_t', '$quantity_d', '$hoavan[$cProduct]', '$logo[$cProduct]', '$matma[$cProduct]', '$order');");
				}
				$cProduct++;
			}
		}
		$date = strtotime($uContract[6]);
		$datelog = date('d/m/Y', time());
		$userlog = $_SESSION['username'];
		$cTamung = str_replace(',', '', $uContract[8]);
		mysql_query("UPDATE `contract` SET `name` = '$uContract[1]', `tax` = '$uContract[3]', `address` = '$uContract[2]', `director` = '$uContract[5]', `position` = '$uContract[7]', `date` = '$date', `contract` = '$uContract[4]', `datelog` = '$datelog', `userlog` = '$userlog', `niemphong` = '$uContract[12]', `tamung` = '$cTamung', `ngaygiao` = '$uContract[10]', `marketing` = '$uContract[9]', `note` = '$uContract[11]', `product` = '$cProduct' WHERE `id` = $uContract[0]");
		header('Content-type: application/json');
		echo json_encode(array('return'=>true));
		exit();
	}
}
elseif($get == 'cover'){
	if($do == 'display'){
		$number = $_REQUEST['number'];
		mysql_query("UPDATE `setting` SET `value` = '$number' WHERE `name` = 'cover_display'");
	}
}
?>